<?php

class CustomerException extends Exception {
    
    public function __construct($message, $code = 0) {  
        parent::__construct($message, $code);  
    }  
  
    public function __toString() {  
        return __CLASS__.':['.$this->code.']:'.$this->message.'\n';  
    }

    public static function  ThrowException($message, $code=-1){
    	throw new CustomerException($message, $code);
	}

  
}
function customer_exception_handler($ex){
	try{
		$msg = new Message();
		/*$msg->flag=$ex->getCode();
		$msg->message=$ex->getMessage();
		$msg->failure = true;
		$msg->success = true;*/
		$msg ->setException($ex);
		$conn = AbstractDbUtils::GetOpenedConnection();
		if($conn !=null && $conn->transaction_opened == true){
			$conn->rollBack();
		}

		
		//$filename=APP_PATH.DIRECTORY_SEPARATOR."log".DIRECTORY_SEPARATOR."customer_".date("Ymd").".log";
	    //$h = @fopen($filename,'a+');
    	$log="[".date('Y-m-d H-i-s',time())."]:\t".$ex->getMessage()."\r\n\t";
    	$log.=$ex->getFile()." ";
    	$log.=$ex->getLine();
    	Logs::WriteLog(date("Y-m-d"),$log);
	    /*if($h){
	          @fwrite($h,$log."\r\n\r\n");
	          @fclose($h);
	    }*/

		echo json_encode($msg);
	}
	catch(Exception $ex1) {
        echo $ex1->getMessage();
    }
} 

function customError($error_level,$error_message,$error_file,$error_line,$error_context){
    $filename=APP_PATH.DIRECTORY_SEPARATOR."log".DIRECTORY_SEPARATOR."err_".date("Ymd").".log";
    $h = @fopen($filename,'a+');
	$log="[".date('Y-m-d H-i-s',time())."]:".$error_message."\r\n";
	$log.="\t\t\t\t\t"."  ".$error_file." ";
	$log.=$error_line;
    if($h){
          @fwrite($h,$log."\r\n\r\n");
          @fclose($h);
    }

} 


set_exception_handler("customer_exception_handler");
set_error_handler("customError");
?>